package com.example.shoppingdemo.RKL;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.Utils;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.RKL.Model.MsgModel;
import com.example.shoppingdemo.RKL.Model.SiginModel;
import com.example.shoppingdemo.RKL.Persenter.PersenterLoginAngSigin;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* 注册
* */
public class SigInActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private EditText editText4;
    private EditText editText5;
    private EditText editText6;
    private TextView textView;
    private PersenterLoginAngSigin persenterLoginAngSigin;
    private CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sig_in);
        persenterLoginAngSigin=new PersenterLoginAngSigin();
        Utils.init(this);
        //账号
        editText1=findViewById(R.id.zc_zh_id);
        //验证码
        editText2=findViewById(R.id.zc_yzm_id);
       /* //身份证
        editText3=findViewById(R.id.zc_sfz_id);*/
        //密码
        editText4=findViewById(R.id.zc_mm_id);
        //确认密码
        editText5=findViewById(R.id.zc_mm_id2);
        editText6=findViewById(R.id.zc_yhm_id);
        //用户协议
        checkBox=findViewById(R.id.yhxy_id);

        //注册按钮
        findViewById(R.id.zc_zc_id).setOnClickListener(this);
        //发送验证码
        textView=findViewById(R.id.zc_fsyzm_id);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone=editText1.getText().toString();
                persenterLoginAngSigin.persenterMsg(phone, new PersenterLoginAngSigin.TransferMsg() {
                    @Override
                    public void TransferMsg(List<MsgModel> msgModelList) {
                        String s = msgModelList.get(0).getCode();
                        Toast.makeText(SigInActivity.this, s, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
//  320311770706001

    @Override
    public void onClick(View view) {
switch (view.getId()){
    case R.id.zc_zc_id:
        //账号
            String name=editText1.getText().toString();
        //验证码
            String code=editText2.getText().toString();
        /*//身份证
            String id = editText3.getText().toString();*/
        //密码
            String Password1=editText4.getText().toString();
        //确认密码
            String Password2=editText5.getText().toString();
            if (TextUtils.isEmpty(name)){
                Toast.makeText(SigInActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            }else if (!checkPhone(name)|name.length() != 11){
                Toast.makeText(SigInActivity.this, "手机号格式错误", Toast.LENGTH_SHORT).show();
            }else if (TextUtils.isEmpty(code)){
                Toast.makeText(SigInActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
            }else if (TextUtils.isEmpty(Password1)){
                Toast.makeText(SigInActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            }else if (Password1.length()<=6){
                Toast.makeText(SigInActivity.this, "密码不得小于六位数", Toast.LENGTH_SHORT).show();
            }else if (TextUtils.isEmpty(Password2)){
                Toast.makeText(SigInActivity.this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
            }else if(!TextUtils.equals(Password1,Password2)){
                Toast.makeText(SigInActivity.this, "两次输入密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
            }else if(!checkBox.isChecked()){
                Toast.makeText(SigInActivity.this, "请勾选用户协议", Toast.LENGTH_SHORT).show();
            }else {

                String a=editText2.getText().toString();
                String yhm=editText6.getText().toString();
                persenterLoginAngSigin.persenterSigin(name, Password1, a, yhm, new PersenterLoginAngSigin.TransferSigin() {
                    @Override
                    public void TransferSigin(List<SiginModel> siginModelList) {
                        String s = siginModelList.get(0).getMmsg();
                        if (s .equals("注册成功")) {
                            Intent intent = new Intent(SigInActivity.this, Login_Splash_Activity.class);
                            Toast.makeText(SigInActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            startActivity(intent);
                        }else {
                            Toast.makeText(SigInActivity.this, s, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        break;

        }
    }
    /*23090419821103853X*/
    public static boolean checkPhone(String phone) {
        Pattern pattern = Pattern
                .compile("^(13[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\\d{8}$");
        Matcher matcher = pattern.matcher(phone);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }
}